package com.btpj.shop.service.impl;

import com.btpj.shop.dao.GoodsMapper;
import com.btpj.shop.dto.param.GoodsParam;
import com.btpj.shop.entity.Goods;
import com.btpj.shop.entity.User;
import com.btpj.shop.service.GoodsService;
import com.btpj.shop.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private UserService userService;

    @Override
    @Transactional
    public boolean addGoods(GoodsParam goodsParam) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsParam, goods);
        User currentUser = userService.getUserInfo();
        goods.setCreateUser(currentUser.getId());
        goods.setUpdateUser(currentUser.getId());
        return goodsMapper.insertSelective(goods) > 0;
    }

    @Override
    public Goods getGoods(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean exists(Long id) {
        return getGoods(id) != null;
    }

    @Override
    @Transactional
    public boolean updateGoods(Long id, GoodsParam goodsParam) {
        Goods goods = getGoods(id);
        BeanUtils.copyProperties(goodsParam, goods);
        User currentUser = userService.getUserInfo();
        goods.setUpdateUser(currentUser.getId());
        return goodsMapper.updateByPrimaryKey(goods) > 0;
    }

    @Transactional
    @Override
    public boolean updateGoodsSellStatus(Long[] ids, Byte sellStatus) {
        return goodsMapper.updateGoodsSellStatus(ids, sellStatus) > 0;
    }

    @Override
    @Transactional
    public boolean deleteGoods(Long id) {
        return goodsMapper.deleteByPrimaryKey(id) > 0;
    }
}
